//
//  ViewController.m
//  UI04_事件_触摸_手势
//
//  Created by LGC on 16/3/14.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "TouchView.h"
#import "MyTextField.h"
#import "ImageViewWithGesture.h"
@interface ViewController () <UITextFieldDelegate, TouchViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createTouchView];
    [self textField];
    [self swipGesture];
//    [self createGestureView];
//    [self createImageViewWithGesture];
//    [self createScreenEdgeGesture];
   
}

#pragma mark - 知识点1: 事件
/**
 * UIEvent类, 三种事件: 触摸, 晃动, 远程控制.
 * 本节重点: 触摸.
 * 重写UIResponder 关于触摸的三个方法.
 */

#pragma mark - 知识点2: 触摸事件

- (void)createTouchView {
    TouchView *viewOfRed = [[TouchView alloc]initWithFrame:CGRectMake(20, 50, 300, 60)];
    
    viewOfRed.backgroundColor = [UIColor redColor];
    
    /** 代理人 */
    viewOfRed.delegate = self;
    
    viewOfRed.tag = 100;
    
    [self.view addSubview:viewOfRed];
    
    [viewOfRed release];
    
   
}

- (void)textField {
    MyTextField *textFieldOfUser = [[MyTextField alloc]initWithFrame:CGRectMake(20, 150, 300, 60)];
    
    textFieldOfUser.text = @"请输入用户名";
    
    textFieldOfUser.backgroundColor = [UIColor blueColor];
    
    //设置代理人
    textFieldOfUser.delegate = self;
    
    [self.view addSubview:textFieldOfUser];
    
    [textFieldOfUser release];
    
}

#pragma mark -  点击空白处, 释放键盘.
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    
//    [super touchesEnded:touches withEvent:event];
//    
//    [self.view endEditing:YES];
//    
//}

#pragma  mark -  TextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}


#pragma mark -  TouchViewDelegate
- (void)exchangeColor:(TouchView *)touchView {
    
    self.view.backgroundColor = touchView.backgroundColor;
    
    [self.view viewWithTag:100].backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 256.f green:arc4random() % 255 / 256.f blue:arc4random() % 255 / 256.f alpha:1];
    
}

#pragma mark - 知识点3: 响应者链

/**
 * UIView的方法. userInteractionEnabled 用户交互.
 *
 * UIImageView , UILabel 用户交互默认是关闭的.
 */

#pragma mark - 知识点4: 手势

/** ViewController的轻拍手势 */
- (void)createGestureView {
    
    UIView *viewOfGesture = [[UIView alloc]initWithFrame:CGRectMake(20, 250, 300, 60)];
    
    viewOfGesture.backgroundColor = [UIColor grayColor];
    
    [self.view  addSubview:viewOfGesture];
    
    [viewOfGesture release];
    
    // 创建轻拍手势对象.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
    
    // 设置相关属性(API)
    tap.numberOfTapsRequired = 2;
    
    tap.numberOfTouchesRequired = 1;
    
    // 添加到相关视图上.
    [viewOfGesture addGestureRecognizer:tap];
    
    [tap release];
}

- (void)handleTap: (UITapGestureRecognizer *)tap {
    
    NSLog(@"%@", tap);
    
    NSLog(@"轻拍");
}


#pragma mark - 知识点5: 自定义ImageView(带手势)

- (void)createImageViewWithGesture {
    ImageViewWithGesture *imageView = [[ImageViewWithGesture alloc]initWithImage:[UIImage imageNamed:@"iconfont-xiaogougou.png"]];
    
    imageView.frame = CGRectMake(0, 0, 200, 200);
    imageView.center = self.view.center;
    
    [self.view addSubview:imageView];
    
    [imageView release];
}

#pragma mark - 屏幕边缘手势
- (void)createScreenEdgeGesture {
    
    UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer  alloc]initWithTarget:self action:@selector(handleEdge:)];
    
    [self.view addGestureRecognizer:edge];
    
    edge.edges = UIRectEdgeLeft;
    
    [edge release];
    
    
}

- (void)handleEdge :(UIScreenEdgePanGestureRecognizer *)edge {
    
    NSLog(@"屏幕边缘");
    
}



#pragma mark - 知识点6: UIView的TransForm属性
/**
 * TransForm属性主要设置view的 位移, 缩放, 旋转.
 * 详见 ImageViewWithGesture.m
 */


#pragma mark - 清扫空白处回收键盘.
- (void)swipGesture {
    UISwipeGestureRecognizer *swip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwip:)];
    
    [self.view addGestureRecognizer:swip];
    
    swip.direction = UISwipeGestureRecognizerDirectionDown;
    
    [swip release];
    
}

- (void)handleSwip:(UISwipeGestureRecognizer *)swip {
    
    [self.view endEditing:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
